<template>
  <div class="dialog">
    <el-dialog
      :title="info.isadd ? '添加轮播图' : '编辑轮播图'"
      :before-close="cancel"
      :visible.sync="info.isshow"
      center
    >
     <el-form :model="bannerInfo" :rules="rules" ref="bannerInfo">
        <el-form-item
          label="轮播图标题"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="图片"
          :label-width="formLabelWidth"
        >
    
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :auto-upload="false"
            :on-change="onChange"
            :file-list="fileList"
            :limit="1"
            :on-exceed="onExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="addInfo" v-if="info.isadd" type="primary"
          >添 加</el-button
        >
        <el-button @click="editInfo" v-else type="primary">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapActions,mapGetters} from 'vuex'
import { banneradd, bannerinfo,banneredit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
        bannerInfo: {
        title: "", 
        img: "", 
        status: 1 
      },
       rules: {
        title: [
          { required: true, message: "请输入轮播图名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
      dialogVisible: false,
      dialogImageUrl: "",
      imgUrl: "",
      fileList: [],
      formLabelWidth: "120px",
    };
  },
   mounted() {
    this.getbannerlist();
  },
  methods: {
    ...mapActions({
      getbannerlist: "banner/getbannerAction",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.empty();
    },
    empty() {
      this.bannerInfo={
        title: "", 
        img: "", 
        status: 1 
      }
       this.$refs.bannerInfo.resetFields();
      this.fileList =[]
    },
    onExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
     onPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    onChange(file, fileList) {
      console.log(file, "文件详情，change时候");
      this.imgUrl = file.raw;
    },
    addInfo() {
     this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          this.bannerInfo.img = this.imgUrl;
          let file = new FormData();
          for (let i in this.bannerInfo) {
            file.append(i, this.bannerInfo[i]);
          }
          banneradd(file).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
               this.getbannerlist();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    lookup(id) {
     bannerinfo({ id }).then(res => {
        if (res.code == 200) {
          this.bannerInfo = res.list;
          this.bannerInfo.id = id;
          //判断图片是否存在 实现图片的回显
          this.fileList = this.bannerInfo.img
            ? [{ url: this.$imgUrl + this.bannerInfo.img }]
            : [];
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    editInfo() {
      this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          this.bannerInfo.img = this.imgUrl ? this.imgUrl : this.bannerInfo.img;
          let file = new FormData();
          for (let i in this.bannerInfo) {
            file.append(i, this.bannerInfo[i]);
          }
          banneredit(file).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getbannerlist();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.dialog {
  position: absolute;
  top: 50%;
  left: 50%;
}
